from datetime import datetime

from flask import Blueprint, request
from flask_jwt_extended import current_user, jwt_required
from flask_sqlalchemy.pagination import Pagination

from pear_admin.extensions import db
from pear_admin.orms import StudentORM

student_api = Blueprint("student", __name__)


@student_api.get("/student")
def student_list():
    page = request.args.get("page", default=1, type=int)
    per_page = request.args.get("limit", default=10, type=int)
    q = db.select(StudentORM)

    pages: Pagination = db.paginate(q, page=page, per_page=per_page)

    return {
        "code": 0,
        "msg": "获取学生数据成功",
        "data": [item.json() for item in pages.items],
        "count": pages.total,
    }


@student_api.post("/student")
def create_student():
    data = request.get_json()
    if data["id"]:
        del data["id"]
    student = StudentORM(**data)

    student.save()
    return {"code": 0, "msg": "新增学员成功"}


@student_api.put("/student/<int:uid>")
def change_student(uid):
    data = request.get_json()
    del data["id"]

    student_obj = StudentORM.query.get(uid)
    for key, value in data.items():
        # if key == "create_at":
        #     value = datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
        setattr(student_obj, key, value)
    student_obj.save()
    return {"code": 0, "msg": "修改学生信息成功"}


# @user_api.delete("/user/<int:rid>")
# def del_user(rid):
#     user_obj = UserORM.query.get(rid)
#     user_obj.delete()
#     return {"code": 0, "msg": "删除用户成功"}
#
#
# @user_api.get("/user/user_role/<int:uid>")
# def get_user_role(uid):
#     user: UserORM = db.session.execute(
#         db.select(UserORM).where(UserORM.id == uid)
#     ).scalar()
#
#     wn_role_list = [r.id for r in user.role_list]
#
#     return {
#         "code": 0,
#         "msg": "返回角色权限数据成功",
#         "data": wn_role_list,
#     }
#
#
# @user_api.put("/user/user_role/<int:rid>")
# def change_user_role(rid):
#     role_ids = request.json.get("rights_ids", "")
#     role_list = role_ids.split(",")
#
#     user: UserORM = db.session.execute(
#         db.select(UserORM).where(UserORM.id == rid)
#     ).scalar()
#     role_obj_list = db.session.execute(
#         db.select(RoleORM).where(RoleORM.id.in_(role_list))
#     ).all()
#     user.role_list = [r[0] for r in role_obj_list]
#     user.save()
#     return {"code": 0, "msg": "授权成功"}
#
#
# @user_api.get("/user/profile")
# @jwt_required()
# def user_profile():
#     return {
#         "code": 0,
#         "msg": "获取个人数据成功",
#         "data": current_user.json(),
#     }
#
#
#


















